//
//  WWMainController.m
//  Shopping
//
//  Created by 张湾湾 on 16/8/18.
//  Copyright © 2016年 张湾湾. All rights reserved.
//

#define MAS_SHORTHAND_GLOBALS
#define MAS_SHORTHAND_GLOBALS
#import <Masonry/Masonry.h>

#import "WWMainController.h"
#import "WWHomeViewController.h"
#import "WWSupermarketViewController.h"
#import "WWShoppingCartViewController.h"
#import "WWTabBar.h"

#import  "WWShopCartDetilController.h"
#import "WWMeViewController.h"
#import "WWGoodsDetailViewController.h"
@interface WWMainController ()<UITabBarControllerDelegate>


@end

@implementation WWMainController
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    //要拦截点击的viewController
    NSInteger currentIndex = [self.childViewControllers indexOfObject:viewController];
    if (currentIndex == 2) {
        //重新弹出新的VC
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:[WWShopCartDetilController new]];
        [self presentViewController:nav animated:YES completion:nil];
        return NO;
    }

    return YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;

   
    WWTabBar * wwTabBar = [[WWTabBar alloc]init];
    [self setValue:wwTabBar forKey:@"tabBar"];


    [self addChildViewController:[WWHomeViewController new] title:@"首页" imageName:@"v2_home"];
//    [self addChildViewController:[WWSupermarketViewController new] title:@"闪电超市" imageName:@"v2_order"];
    [self addChildViewController:[WWGoodsDetailViewController new] title:@"闪电超市" imageName:@"v2_order"];
      [self addChildViewController:[WWShoppingCartViewController new] title:@"购物车" imageName:@"shopCart"];
    [self addChildViewController:[WWMeViewController new] title:@"我的" imageName:@"v2_my"];
}

#pragma mark
#pragma mark - 创建tab
- (void)addChildViewController:(UIViewController *)childController title:(NSString*)title imageName:(NSString*)imageName{
    
    childController.title = title;
    [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor darkGrayColor]} forState:UIControlStateSelected];
    childController.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_r",imageName]
                                                 ] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:childController];
    
    [self addChildViewController:nav];
}



@end
